

package com.nn.smart.bus.controller;

import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.ExcelUtils;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.commons.tools.validator.AssertUtils;
import com.nn.smart.bus.commons.tools.validator.ValidatorUtils;
import com.nn.smart.bus.commons.tools.validator.group.AddGroup;
import com.nn.smart.bus.commons.tools.validator.group.UpdateGroup;
import com.nn.smart.bus.commons.tools.validator.group.DefaultGroup;
import com.nn.smart.bus.dto.NnclNnemployeesDTO;
import com.nn.smart.bus.excel.NnclNnemployeesExcel;
import com.nn.smart.bus.service.NnclNnemployeesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 公交从业人员
 *
 * @author nanning nanning@123.com
 * @since v1.0.0 2021-08-25
 */
@RestController
@RequestMapping("nnclnnemployees")
public class NnclNnemployeesController {

    @Autowired
    private NnclNnemployeesService nnclNnemployeesService;

    @GetMapping("page")
    public Result<PageData<NnclNnemployeesDTO>> page(@RequestParam Map<String, Object> params){
//        PageData<NnclNnemployeesDTO> page = nnclNnemployeesService.page(params);
        PageData<NnclNnemployeesDTO> page = nnclNnemployeesService.getPhrasePage(params);
        return new Result<PageData<NnclNnemployeesDTO>>().ok(page);
    }

    @GetMapping("{id}")
    public Result<NnclNnemployeesDTO> get(@PathVariable("id") String id){
        NnclNnemployeesDTO data = nnclNnemployeesService.get(id);
        return new Result<NnclNnemployeesDTO>().ok(data);
    }

    @PostMapping
    public Result save(@RequestBody NnclNnemployeesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        nnclNnemployeesService.save(dto);
        return new Result();
    }

    @PutMapping
    public Result update(@RequestBody NnclNnemployeesDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        nnclNnemployeesService.update(dto);
        return new Result();
    }

    @DeleteMapping
    public Result delete(@RequestBody String[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        nnclNnemployeesService.delete(ids);
        return new Result();
    }

    @GetMapping("export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<NnclNnemployeesDTO> list = nnclNnemployeesService.list(params);
        ExcelUtils.exportExcelToTarget(response, null, list, NnclNnemployeesExcel.class);
    }

}
